package com.typing.controller;

import com.typing.entity.AdminEntity;
import com.typing.entity.UserEntity;
import com.typing.entity.WordsEntity;
import com.typing.service.AdminService;
import com.typing.service.UserService;
import com.typing.service.WordsService;
import com.typing.utils.Result;
import com.typing.utils.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/adminSystem")
public class AdminSystemController {

	@Resource
	WordsService wordService;

	@Resource
	UserService userService;

	@Resource
	AdminService adminService;


	/**
	 * 重置用户密码
	 *
	 * @param user
	 * @return
	 */
	@PostMapping("/updateUserPwd")
	public Result updateUserPwd(@RequestBody UserEntity user) {
		//	手机号格式判断
		if (!Tools.isPhone(user.getMobilePhone())) {
			return new Result().mobilePhoneError();
		}
		//	密码格式判断
		if (!Tools.isPassword(user.getPassword())) {
			return new Result().pwFormatError();
		}
		//	进行注册查询
		UserEntity users = userService.isRegister(user);
		if (users == null) {
			return new Result().notRegisteredError();
		}
		//	进行密码更改
		int count = userService.updatePassword(user);
		if (count != 1) {
			return new Result().systemError();
		}
		return new Result().ok("用户密码修改成功");
	}

	/**
	 * 获取全部用户
	 *
	 * @return
	 */
	@GetMapping("/getAllUser")
	public Result getAllUser() {
		List<UserEntity> userEntities = userService.selectAllUser();
		return new Result().ok(userEntities);
	}

	/**
	 * 添加单词
	 *
	 * @param word
	 * @return
	 */
	@PostMapping("/addWord")
	public Result addWord(@RequestBody WordsEntity word) {
		//	英文非空判断
		if (!Tools.isRightParam(word.getEnglish())) {
			return new Result().wordFormatError();
		}
		//	首字母非空判断
		if (!Tools.isRightParam(word.getFirstWord())) {
			return new Result().firstWordFormatError();
		}
		//	中文非空判断
		if (!Tools.isRightParam(word.getChinese())) {
			return new Result().chineseFormatError();
		}
		//	查看有没有这个单词
		WordsEntity words = wordService.hasWord(word.getEnglish());
		if (words != null) {
			return new Result().wordIsRegistered();
		}
		int count = wordService.addWord(word);
		if (count != 1) {
			return new Result().systemError();
		}
		return new Result().ok("单词添加成功");
	}

	/**
	 * 删除单词
	 *
	 * @param word
	 * @return
	 */
	@PostMapping("/deleteWord")
	public Result deleterWord(@RequestBody WordsEntity word) {
		//	英文非空判断
		if (!Tools.isRightParam(word.getEnglish())) {
			return new Result().wordFormatError();
		}
		//	查看是否有这个单词
		WordsEntity words = wordService.hasWord(word.getEnglish());
		if (words == null) {
			return new Result().wordNotRegister();
		}
		//	进行单词的删除
		int count = wordService.deleteWord(word);
		if (count != 1) {
			return new Result().systemError();
		}
		return new Result().ok("单词删除成功");
	}

	/**
	 * 更改单词信息
	 *
	 * @param word
	 * @return
	 */
	@PostMapping("/updateWord")
	public Result updateWord(@RequestBody WordsEntity word) {
		//	英文非空判断
		if (!Tools.isRightParam(word.getEnglish())) {
			return new Result().wordFormatError();
		}
		//	首字母非空判断
		if (!Tools.isRightParam(word.getFirstWord())) {
			return new Result().firstWordFormatError();
		}
		//	中文非空判断
		if (!Tools.isRightParam(word.getChinese())) {
			return new Result().chineseFormatError();
		}
		//	查看是否有这个单词
		WordsEntity words = wordService.hasWord(word.getEnglish());
		if (words == null) {
			return new Result().wordNotRegister();
		}
		int count = wordService.updateWord(word);
		if (count != 1) {
			return new Result().systemError();
		}
		return new Result().ok("单词修改成功");
	}


	/**
	 * 添加管理员
	 *
	 * @param admin
	 * @return
	 */
	@PostMapping("/addAdmin")
	public Result addAdmin(@RequestBody AdminEntity admin) {
		//	对账号格式进行判断
		if (!Tools.isRightParam(admin.getUsername())) {
			return new Result().adminFormatError();
		}
		//	对密码格式进行判断
		if (!Tools.isPassword(admin.getPassword())) {
			return new Result().pwFormatError();
		}
		//	判断有没有注册
		AdminEntity adminEntities = adminService.isRegister(admin);
		if (adminEntities != null) {
			return new Result().adminIsRegistered();
		}
		//	进行注册
		int i = adminService.addAdmin(admin);
		if (i != 1) {
			return new Result().systemError();
		}
		return new Result().ok("管理员添加成功");
	}


}
